//
//  receiveGiftSend.m
//  tcpoctest
//
//  Created by gump on 5/21/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "receiveGiftSend.h"

#import "GiftInfoDataSource.h"

#import "ChatContentInfo.h"

#import "chatDataSource.h"
#import "XMMessageInfo.h"
#import "chatuserdb.h"
#import "MessageDataSource.h"
#import "messagedb.h"

@implementation receiveGiftSend

-(void)processreceive
{
    //返回 26char(1),3char(1),礼物ID长度char(1),
    //礼物idchar(x)，返回值char(1)、1赠送成功\2金币不足,
    //收礼人名称长度char(1),收礼人名称char(x)，int数量
    NSString *strgiftid = [logicsb ReadUnencodeString];
    
    int ibacktype = [logicsb ReadChar];
    [[DataControl shareControl].giftInfoDS setSendBackState:ibacktype];
    
    NSString *strmail = [logicsb ReadUnencodeString];
    
    int igift = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
    
    NSString *strid = [logicsb ReadUnencodeString];
    NSString *strtime = [logicsb ReadUnencodeString];
    
    if (ibacktype == GSBS_SUCCESS) {
        
        ChatContentInfo *cci = [[ChatContentInfo alloc] init];
        cci.chatType = CT_GIFT;
        cci.giftid = strgiftid;
        cci.igiftNum = igift;
        cci.mail = strmail;
        cci.isSelf = true;
        cci.strid = strid;
        cci.strChatDate = strtime;
        cci.iSequence = getLocalSequence();
        cci.strsendmail = strmail;
        cci.ichatReadState = CRS_SENDSUCCESS;
        
        
        XMMessageInfo *minfo = [[XMMessageInfo alloc] init];
        minfo.strmail = strmail;
        minfo.messageType = XMMT_CHAT;
        minfo.mbAlreadayArgee = false;
        minfo.mindex = [[DataControl shareControl].messageDS GetMagArrCount];
        minfo.strmsgid = [[DataControl shareControl].messageDS getMsgID];
        
        XMMessageInfo *mexits = [[MessageManager sharedManager] getMessage:minfo];
        
        if (mexits == nil)
        {
            minfo.strtime = cci.strChatDate;
            
            [[MessageManager sharedManager] AddMessage:minfo];
            NSMutableArray *chatshowarr = [[NSMutableArray alloc] init];
            [[DataControl shareControl].chatDS SetChatDicArr:chatshowarr withKey:minfo.strmail];
            [[DataBaseCtrol shareControl].chatuserdatabase insertChatDic:minfo.strmail withIsGroup:false];
            
            [chatshowarr addObject:cci];
            
            [[DataControl shareControl].chatDS addChatInfo:cci withID:cci.strid];
            
            [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cci];
        }
        else
        {
            
            NSMutableArray *chatshowarr = [[DataControl shareControl].chatDS GetArrByKey:strmail];
            if (chatshowarr != nil)
            {
                [chatshowarr addObject:cci];
                
                [[DataControl shareControl].chatDS addChatInfo:cci withID:cci.strid];
                
                [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cci];
            }
            
            mexits.strtime = cci.strChatDate;
            [[DataBaseCtrol shareControl].messagedatabase updateMessage:mexits];
            
        }

        
        
    }
    
    [[DataControl shareControl].UICommendDS addUICommand:UC_GIFT_SENDBACK];
    
    [self ShowLog];
}

@end
